﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormAssigment.BUS;
using WinFormAssigment.DAO;
namespace WinFormAssigment
{
    public partial class AttendanceDemo : Form
    {
        public AttendanceDemo()
        {
            InitializeComponent();
        }

        private void AttendanceDemo_Load(object sender, EventArgs e)
        {
                        
        }

        public void FillAttendanceTable(string classID)
        {
            DataTable mytable = AttendanceManager.GetStudentInClass(classID);
            dataGridView1.DataSource = mytable;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GetAllClassByTeacher(Query.userID);
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (chbAttend.Checked)
            {
                int rows = dataGridView1.Rows.Count;
                for (int i = 0; i < rows; i++)
                {
                    dataGridView1.Rows[i].Cells[2].Value = true;
                }
            }
        }

        private void GetAllClass()
        {
            treeView1.Nodes.Clear();

            DataTable table = AttendanceManager.GetAllClassID();
            foreach (DataRow dr in table.Rows)
            {
                TreeNode node = new TreeNode(dr["classID"].ToString().ToUpper());
                treeView1.Nodes.Add(node);
            }
        }
        private void GetAllClassByTeacher(string teacherID)
        {
            treeView1.Nodes.Clear();

            DataTable table = AttendanceManager.GetAllClassIdByTeacher(teacherID);
            foreach (DataRow dr in table.Rows)
            {
                TreeNode node = new TreeNode(dr["classID"].ToString().ToUpper());
                treeView1.Nodes.Add(node);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string classID = treeView1.SelectedNode.Text;
            DataTable mytable = AttendanceManager.GetStudentInClass(classID);

            if (mytable != null)
            {

                dataGridView1.DataSource = mytable;

            }
        }

        // demo select cells check..............................................

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable mytable = AttendanceManager.GetStudentInClass("c1208i");
            dataGridView1.DataSource = mytable;        }

        

        private void chbAttend_CheckedChanged(object sender, EventArgs e)
        {
            if (chbAttend.Checked)
            {
                chbAbsent.Checked = false;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells[2].Value = true;
                }
            }
            else
            {
                chbAbsent.Checked = false;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells[2].Value = false;
                }
            }
        }
        private void chbAbsent_CheckedChanged(object sender, EventArgs e)
        {
            if (chbAbsent.Checked)
            {
                chbAttend.Checked = false;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells[3].Value = true;
                }
            }
            else
            {
                chbAttend.Checked = false;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells[3].Value = false;
                }
            }
        }        
        private void button3_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[0];
            MessageBox.Show(row.Cells[2].Value.ToString());

        }
        // Xy ly diem danh
        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            int index = e.ColumnIndex;
            if (index >= 2 && index <= 3)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells[index];
                if (checkBox.Value != null)
                {
                    bool isChecked = Convert.ToBoolean(checkBox.Value);
                    if (isChecked)
                    {
                        for (int i = 2; i <= 3; i++)
                        {
                            if (i != index)
                            {
                                row.Cells[i].Value = false;
                            }
                        }
                    }
                }
                dataGridView1.Invalidate();
            }

        }

        
        

    }
}
